import 'package:intl/intl.dart';
import 'package:memo/state/model/todo.dart';
import 'package:sqflite/sqflite.dart';
import 'package:uuid/uuid.dart';

class WorkRecord {
  String id;
  bool morningFlag;
  bool nightFlag;
  DateTime time;
  List<Todo> todoList;

  WorkRecord(
      {this.id, this.time, this.morningFlag, this.nightFlag, this.todoList}) {
    this.id = this.id == null ? Uuid().v4().replaceAll("-", "") : this.id;
    this.time = this.time == null ? DateTime.now() : this.time;
    this.todoList = this.todoList == null ? [] : this.todoList;
    this.nightFlag = this.nightFlag == null ? false : this.nightFlag;
    this.morningFlag = this.morningFlag == null ? false : this.morningFlag;
  }

  static WorkRecord fromMap(Map<String, dynamic> map) {
    int time = map["time"];
    return WorkRecord(
        id: map["id"],
        morningFlag: map["morningFlag"] == 1 ? true : false,
        nightFlag: map["nightFlag"] == 1 ? true : false,
        time: DateTime.fromMillisecondsSinceEpoch(time * 1000));
  }

  static List<WorkRecord> fromMaps(List<Map<String, dynamic>> list) {
    return list.map((w) => WorkRecord.fromMap(w)).toList();
  }

  @override
  String toString() {
    return 'WorkRecord{id: $id, morningFlag: $morningFlag, nightFlag: $nightFlag, time: $time, todoList: $todoList}';
  }
}

const workRecordTableName = "work_record";

const workRecordSQL = """CREATE TABLE $workRecordTableName (
    id CHAR(32) PRIMARY KEY NOT NULL,
    morningFlag SMALLINT NOT NULL,
    nightFlag SMALLINT NOT NULL,
    time INT NOT NULL
  )""";

const insertWorkRecordSQL = """
    insert into $workRecordTableName (id, morningFlag, nightFlag, time) values (?,?,?,?)
  """;

const updateWorkRecordSQL = """
    update $workRecordTableName set morningFlag=?, nightFlag=? where id = ?
  """;

const queryWorkRecordSQL = """
  select id, morningFlag, nightFlag, time from $workRecordTableName order by time desc limit ?,?
  """;

const queryWorkRecordByTimeSQL = """
  select id, morningFlag, nightFlag, time from $workRecordTableName where time between ? and ? limit 1
""";

Future<void> insertWorkRecord(Database db, WorkRecord r) async {
  await db.rawInsert(insertWorkRecordSQL, [
    r.id,
    r.morningFlag ? 1 : 0,
    r.nightFlag ? 1 : 0,
    r.time.millisecondsSinceEpoch ~/ 1000
  ]);
}

Future<void> updateWorkRecord(
    Database db, String id, bool morningFlag, bool nightFlag) async {
  await db.rawUpdate(
      updateWorkRecordSQL, [morningFlag ? 1 : 0, nightFlag ? 1 : 0, id]);
}

Future<List<WorkRecord>> queryWorkRecord(Database db,
    {int start = 0, int len = 12}) async {
  final list = await db.rawQuery(queryWorkRecordSQL, [start, len]);
  print(list);
  return WorkRecord.fromMaps(list);
}

Future<WorkRecord> queryWorkRecordByDay(Database db, DateTime start, DateTime end) async {
  final record  = await db.rawQuery(queryWorkRecordByTimeSQL, [
    start.millisecondsSinceEpoch ~/ 1000,
    end.millisecondsSinceEpoch ~/ 1000
  ]);
  return record.isEmpty ? null : WorkRecord.fromMap(record[0]);
}
